package org.znxs.znmanus.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.retry.NonTransientAiException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.TreeMap;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(NonTransientAiException.class)
    public Map<String, Object> handleNonTransientAiException(NonTransientAiException ex) {
        Map<String, Object> errorResponse = new TreeMap<>();
        errorResponse.put("timestamp", LocalDateTime.now());
        errorResponse.put("status", HttpStatus.UNAUTHORIZED.value());
        errorResponse.put("error", "Unauthorized");
        errorResponse.put("message", "请检查ai配置，apikey配置,AI服务调用失败：" + ex.getMessage());
        errorResponse.put("exception", ex.getClass().getSimpleName());

        // 可以记录日志
         log.error("AI服务调用失败,请检查ai配置，apikey配置", ex);

        return errorResponse;
    }
}